/**
 * calculate and return wifi contex aware
 */
package com.google.zxing.client.android;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiManager;

/**
 * @author aabramov
 * 
 */
public final class ContexWiFiManager {

	private static final String TAG = ContexWiFiManager.class.getSimpleName();

	private final Activity activity;
	private List<ScanResult> wifiScanResult;

	public static String getTag() {
		return TAG;
	}

	public Activity getActivity() {
		return activity;
	}

	private String rezultWiFiContext = "";

	public ContexWiFiManager(Activity activity) {
		this.activity = activity;
		checkLocationContext();

	}

	// Warning
	// Binding method to calculate the wifi contex aware
	public void checkLocationContext() {
		Context c = activity.getApplicationContext();

		WifiManager wifi = (WifiManager) c
				.getSystemService(Context.WIFI_SERVICE);
		if (wifi.isWifiEnabled()) {

			setWifiScanResult(wifi.getScanResults());
			if (wifiScanResult != null && !wifiScanResult.isEmpty()) {

			}
		}

	}

	// Return executed result string
	public String buildResultWifiContext() {
		int count = 0;
		if (getWifiScanResult() != null && !getWifiScanResult().isEmpty()) {

			// 3 uses only the network with the highest signal
			for (ScanResult r : getWifiScanResult()) {
				if (count < 3) {
					String ssidEncode ="";
					try {
						ssidEncode = URLEncoder.encode(r.SSID,"UTF-8");
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
					rezultWiFiContext += "&ssid=" + ssidEncode + "&mac=" + r.BSSID
							+ "&RSSI=" + r.level;
					count++;

				} else {
					break;
				}
			}
		}
		return rezultWiFiContext;

	}

	private String getRezultWiFiContext() {

		return rezultWiFiContext;
	}

	public List<ScanResult> getWifiScanResult() {
		return wifiScanResult;
	}

	public void setWifiScanResult(List<ScanResult> wifiScanResult) {
		this.wifiScanResult = wifiScanResult;
	}

}
